package com.hr.exception;

import com.hr.dto.JsonResp;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLException;
import java.sql.SQLIntegrityConstraintViolationException;


@RestControllerAdvice //拦截异常并统一处理
public class GlobalExceptionHandler
{
    @ExceptionHandler(Exception.class)
    public JsonResp Exception(Exception e) {
        e.printStackTrace();

        if (e instanceof SQLIntegrityConstraintViolationException) {
            return JsonResp.badRequest("该数据有关联数据，操作失败!");
        }
        else if(e instanceof SQLException)
            return JsonResp.badRequest("数据库异常，操作失败!");
        else
            return JsonResp.badRequest(e.getMessage());
    }
}